<?php

/**
 * Created by PhpStorm.
 * User: GadflyBSD
 * Date: 16/5/26
 * Time: 17:18
 */
class ManageModel extends PublicModel{
	/**
	 * @desc transfer 汇付天下转账模型
	 * @param $data
	 *
	 * @return string
	 */
	public function transfer($data){
		Vendor('ChinaPnr');
		$pnr = new ChinaPnr();
		$post = array(
			'out_cust_id'   => '6666000000000268',
			'out_acct_id'   => '316',
			'in_cust_id'    => $data['in_cust_id'],
			'in_acct_id'    => $data['in_acct_id'],
			'transfer_amt'  => $data['money'],
		);
		return $pnr->transfer($post);
	}

	/**
	 * @desc balance 汇付天下查询余额模型
	 * @param $data
	 *
	 * @return string
	 */
	public function balance($data){
		Vendor('ChinaPnr');
		$pnr = new ChinaPnr();
		$post = array(
			'user_cust_id'  => $data['user_cust_id'],
			'extension'     => json_encode_plus(array('sid' => $data['sid'], 'uid' => $data['uid'])),
		);
		return $pnr->balance($post);
	}

	public function setVipLimit($post){
		$data = array(
			'title'         => $post['title'],
			'suite'         => $post['suite'],
			'telephone'     => $post['telephone'],
			'fee'           => ($post['fee']/100),
			'vip_limit'     => $post['vip_limit'],
			'last_changes'  => 'shopRefusal',
			'last_info'     => '系统管理员审核签约时限至'.date('Y年m月d日', $post['vip_limit']),
		);
		$save = D('Public')->curd(array('type' => 'save', 'model' => M('WangkaShop'), 'data' => $data, 'where' => 'sid='.$post['sid']));
		if($save['errcode'] == 0){
			return array('errcode' => '0', 'content' => $data['last_info']);
		}else{
			return $save;
		}
	}

	/**
	 * 结算APP端订单数据
	 * @return array|mixed
	 */
	public function settleOrder(){
		$sync = $this->syncOderData();
		if($sync['errcode'] == '0'){
			$data = M()->query('CALL settle_proc()');
			if(is_array($data)){
				return array('errcode' => 0, 'data' => $data);
			}else{
				return array('errcode' => 30114);
			}
		}else{
			return $sync;
		}
	}

	/**
	 * 获得商户结算数据列表
	 * @param $start
	 * @param $end
	 *
	 * @return array
	 */
	public function getSettleList($start, $end){
		return $this->curd(array(
			'type'  => 'select',
			'model' => M('WangkaSettleView'),
			'where' => 'settlement_day BETWEEN "'.$start.'" AND "'.$end.'"',
		));
	}

	/**
	 * 解除尽调员绑定数据
	 * @param $bid
	 *
	 * @return array
	 */
	public function removeSurveyBind($bid){
		$data = array();
		return $this->curd(array(
			'type'  => 'save',
			'model' => M('WakaBind'),
			'where' => 'bid='.$bid,
			'data'  => $data,
		));
	}

	/**
	 * 同步当天指定时间的APP端扫码支付的订单数据
	 * @return array
	 */
	private function syncOderData($time=' 16:00:00'){
		date_default_timezone_set(PRC);
		$dateline = strtotime(date('Y-m-d').$time);
		if(time() >= $dateline){
			$where = array('status_flag' => array('eq', 0), 'delete_flag' => array('eq', 0), 'pay_dateline' => array('elt', $dateline));
			$order = M('ScancodePayView','t_','mysql://weixin_user:weixin_HYCT0871@120.25.67.227/stages')->where($where)->select();
			$idin = array();
			foreach($order AS $key => $val){
				$idin[$key] = $val['id'];
			}
			$url = 'http://admin.mywoka.com/stages/realnameclient/checkRealNameInfoCallback.action';
			$return = $this->curlData($url, array('idin' => implode("', '", $idin)), 'POST');
			if($return['success'])
				return $this->curd(array(
					'type'  => 'addAll',
					'model' => M('WangkaOrder'),
					'data'  => $order,
				));
			else
				return array('errcode' => 30115);
		}else{
			return array('errcode' => 30113);
		}

	}

}